package shuangxianLikedList;


import java.util.Scanner;

public class Test {
    public static void main(String[] args) {
        DoubleLinkedList d = new DoubleLinkedList();
        d.addByAge(new Node("陈俊华",20));
        d.addByAge(new Node("陈",18));
        d.addByAge(new Node("林",17));
        d.showList(1);
        d.deleteByNameAndAge(new Node("林",17));
        d.showList(1);
        System.out.println("删除最后一个");
        d.deleteByNameAndAge(new Node("陈俊华",20));
        d.showList(1);
    }
}
class Node{
    public String name;
   public int age;
   public Node pre;
   public Node next;

    public Node() {
    }

    public Node(String name, int age) {
        this.name = name;
        this.age = age;
    }

    @Override
    public String toString() {
        return "Node{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}
class DoubleLinkedList{

    private Node head = new Node("null",0);
    //添加
    public void addByAge(Node node){
        Node temp = head;
        boolean isFlag=false;
        while (true){
            if (temp.next==null){
                isFlag=true;
                break;
            }else if(temp.next.age>node.age){
                break;
            }
            temp=temp.next;
        }
        if (isFlag){

        }
        if (isFlag){
            temp.next=node;
            node.pre=temp;
        }else{

            temp.next.pre=node;
            node.next=temp.next;
            temp.next=node;
            node.pre=temp;
        }


    }
    //遍历
    public void showList(int n){
        switch (n){
            case 1:
                Node temp = head;
                if (temp.next==null){
                    throw new RuntimeException("您要输出的链表为空...");
                }
                while (temp.next!=null){
                    System.out.println(temp.next);
                    temp=temp.next;
                }
                break;
            case 2:
                Node temp1 = head;
                if (temp1.next==null){
                    throw new RuntimeException("您要输出的链表为空...");
                }
                while (temp1.next!=null){
                    temp1=temp1.next;
                }
                while (temp1.pre!=null){
                    System.out.println(temp1.next);
                    temp1=temp1.pre;
                }
                break;
            default:
                System.out.println("输入错误,程序结束");
                break;
        }
    }
    //修改通过名字
    public void update(Node node){
        if(head.next==null){
            System.out.println("无数据无法修改");
            return;
        }
        Node temp = head.next;
        while (true){
            if(temp.name.equals(node.name)){
                Scanner scanner= new Scanner(System.in);
                System.out.println("输入空格后回车均表示不修改保留原值");
                System.out.println("原名为："+temp.name);
                System.out.print("您要将名字修改为:");
                String name = scanner.nextLine();
                if(name!=null && !name.contains(" ")){
                    temp.name=name;
                    System.out.println("名字修改完成为:"+temp.name);
                }else {
                    System.out.println("您已经取消修改,现名为:"+temp.name);
                }
                System.out.println("您要将其昵称改为：");
                int age = scanner.nextInt();
                if(age!=0){
                    temp.age=age;
                    System.out.println("年龄修改完成为"+temp.age);
                }else {
                    System.out.println("您已经取消修改,现年龄为:"+temp.age);
                }
                break;
            }
            temp=temp.next;
            if(temp==null){
                System.out.println("没有找到该名学生");
                return;
            }
        }
    }
    //删除
    public void deleteByNameAndAge(Node node){
        Node temp = head.next;
        if(temp==null){
            System.out.println("改链表为空");
            return;
        }
        while (!(temp.name.equals(node.name)&&temp.age== node.age)){
            temp=temp.next;
        }
        if (temp.next==null){
            temp.pre.next=null;
        }else {
            temp.next.pre=temp.pre;
            temp.pre.next=temp.next;
        }

    }
}



